package com.ssss.danli;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 静态内部类何时初始化
 *
 * 1.静态内部类和
 */
public class OuterClass {

    public static long OUTER_DATE = System.currentTimeMillis();

    /**
     * 静态代码块
     */
    static {
        System.out.println("外部类静态块加载时间："+System.currentTimeMillis());
    }

    /**
     * 构造方法
     */
    public OuterClass(){
        timeElapsed();
        System.out.println("外部列构造函数时间"+System.currentTimeMillis());
    }

    /**
     * 静态内部类
     */
    static class InnerStaticClass{
        public static long INNER_STATIC_DATE = System.currentTimeMillis();
    }

    /**
     * 普通内部类
     */
    class InnerClass{
        public long INNER_DATE = 0;
        public InnerClass(){
            timeElapsed();
            INNER_DATE = System.currentTimeMillis();
        }
    }


    public static void main(String[] args) throws InterruptedException {
        OuterClass outer = new OuterClass();
        System.out.println("外部类静态变量加载时间："+outer.OUTER_DATE);
        TimeUnit.SECONDS.sleep(1);
        System.out.println("静态内部类加载时间："+InnerStaticClass.INNER_STATIC_DATE);
        TimeUnit.SECONDS.sleep(2);
        System.out.println("普通内部类加载时间："+outer.new InnerClass().INNER_DATE);

    }


    //单纯的为了耗时，来扩大时间差异
    private void timeElapsed() {
        for (int i = 0; i < 10000000; i++) {
            int a = new Random(100).nextInt(), b = new Random(100).nextInt();
            a = a + b;
        }
    }

}
